<template>
  <view class="modal-overlay" v-if="show" @click="handleClose">
    <view class="modal-content" @click.stop>
      <view class="modal-header">
        <text class="modal-title">选择标签</text>
        <view class="close-btn" @click="handleClose">
          <text class="close-icon">×</text>
        </view>
      </view>
      
      <view class="tag-list">
        <view 
          class="tag-item" 
          v-for="tag in tags" 
          :key="tag.id"
          :class="{ active: selectedTags.includes(tag.id) }"
          @click="toggleTag(tag.id)"
        >
          <view class="tag-icon" :style="{ backgroundColor: tag.color + '20' }">
            <text class="icon-text">{{ tag.icon || '🏷️' }}</text>
          </view>
          <text class="tag-name">{{ tag.name }}</text>
        </view>
        
        <!-- 创建新标签 -->
        <view class="tag-item create-item" @click="handleCreate">
          <view class="tag-icon create-icon">
            <text class="icon-text">+</text>
          </view>
          <text class="tag-name">创建新标签</text>
        </view>
      </view>
      
      <view class="modal-footer">
        <button class="confirm-btn" @click="handleConfirm">
          确定 ({{ selectedTags.length }})
        </button>
      </view>
    </view>
  </view>
</template>

<script>
export default {
  name: 'TagModal',
  props: {
    show: {
      type: Boolean,
      default: false
    },
    tags: {
      type: Array,
      default: () => []
    },
    selected: {
      type: Array,
      default: () => []
    }
  },
  emits: ['update:show', 'confirm', 'create'],
  data() {
    return {
      selectedTags: []
    }
  },
  watch: {
    selected: {
      handler(newVal) {
        this.selectedTags = [...newVal]
      },
      immediate: true
    }
  },
  methods: {
    handleClose() {
      this.$emit('update:show', false)
    },
    
    toggleTag(tagId) {
      const index = this.selectedTags.indexOf(tagId)
      if (index > -1) {
        this.selectedTags.splice(index, 1)
      } else {
        this.selectedTags.push(tagId)
      }
    },
    
    handleConfirm() {
      this.$emit('confirm', this.selectedTags)
    },
    
    handleCreate() {
      this.$emit('create')
    }
  }
}
</script>

<style scoped>
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
}

.modal-content {
  width: 80%;
  max-height: 70vh;
  background-color: #ffffff;
  border-radius: 16rpx;
  padding: 32rpx;
  animation: fadeIn 0.3s ease-out;
  display: flex;
  flex-direction: column;
}

@keyframes fadeIn {
  from {
    opacity: 0;
    transform: scale(0.9);
  }
  to {
    opacity: 1;
    transform: scale(1);
  }
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 32rpx;
  padding-bottom: 24rpx;
  border-bottom: 1rpx solid #f0f0f0;
}

.modal-title {
  font-size: 32rpx;
  font-weight: 600;
  color: #333333;
}

.close-btn {
  width: 48rpx;
  height: 48rpx;
  display: flex;
  align-items: center;
  justify-content: center;
}

.close-icon {
  font-size: 32rpx;
  color: #999999;
}

.tag-list {
  flex: 1;
  max-height: 50vh;
  overflow-y: auto;
  display: flex;
  flex-wrap: wrap;
  gap: 16rpx;
  margin-bottom: 32rpx;
}

.tag-item {
  display: flex;
  align-items: center;
  padding: 16rpx 24rpx;
  border-radius: 24rpx;
  background-color: #f8f8f8;
  border: 2rpx solid transparent;
  transition: all 0.3s;
}

.tag-item.active {
  background-color: #e6f7ff;
  border-color: #1890ff;
}

.tag-item.create-item {
  border: 2rpx dashed #d9d9d9;
}

.tag-item:active {
  transform: scale(0.95);
}

.tag-icon {
  width: 48rpx;
  height: 48rpx;
  border-radius: 12rpx;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 12rpx;
}

.create-icon {
  background-color: #f0f0f0;
}

.icon-text {
  font-size: 20rpx;
}

.tag-name {
  font-size: 26rpx;
  color: #333333;
}

.modal-footer {
  border-top: 1rpx solid #f0f0f0;
  padding-top: 24rpx;
}

.confirm-btn {
  width: 100%;
  height: 80rpx;
  background-color: #1890ff;
  color: #ffffff;
  border: none;
  border-radius: 40rpx;
  font-size: 28rpx;
  font-weight: 500;
}

.confirm-btn::after {
  border: none;
}
</style> 